package com.itao;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;

/**
 * @author sjt
 * @Date 2025/7/23 14:05
 */
public class Poi {

    public static void main(String[] args) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 7));
        Row row = sheet.createRow(0);
        row.setHeightInPoints(20.3F);
        Cell cell = row.createCell(0);
        // 设置单元格内容
        cell.setCellValue("各司局餐费结算明细单（2025年7月）");
        // 创建单元格样式
        CellStyle style = workbook.createCellStyle();
        // 设置水平居中对齐
        style.setAlignment(HorizontalAlignment.CENTER);
        // 设置垂直居中对齐
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        // 可选：设置字体样式
        Font font = workbook.createFont();
        font.setBold(true);
        font.setFontName("宋体");
        font.setFontHeightInPoints((short)16);
        style.setFont(font);
        // 应用样式到合并单元格
        cell.setCellStyle(style);
        // 自动调整列宽（可选）
        sheet.setColumnWidth(0, 12 * 256);
        sheet.setColumnWidth(1, 10 * 256);
        sheet.setColumnWidth(2, 23 * 256);
        sheet.setColumnWidth(3, 26 * 256);
        sheet.setColumnWidth(4, 11 * 256);
        sheet.setColumnWidth(5, 13 * 256);
        sheet.setColumnWidth(6, 15 * 256);
        sheet.setColumnWidth(7, 12 * 256);


        row = sheet.createRow(1);
        cell = row.createCell(0);
        cell.setCellValue("司局：");
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 6));
        cell = row.createCell(1);
        cell.setCellValue("办公厅");
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.cloneStyleFrom(style);
        cellStyle.setAlignment(HorizontalAlignment.LEFT);
        font = workbook.createFont();
        font.setFontName("微软雅黑");
        font.setFontHeightInPoints((short)12);
        cellStyle.setFont(font);
        // 应用样式到合并单元格
        cell.setCellStyle(cellStyle);
        cell = row.createCell(7);
        cell.setCellValue("单位：元");

//        人员类型	就餐时间	次数	就餐标准	金额	合计金额	经费来源	备注

        cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);
        font = workbook.createFont();
        font.setBold(true);
        font.setFontHeightInPoints((short)12);
        font.setFontName("仿宋_GB2312");
        cellStyle.setFont(font);

        // 应用样式到合并单元格
        row = sheet.createRow(2);
        row.setHeightInPoints(19.5F);
        cell = row.createCell(0);
        cell.setCellValue("人员类型");
        cell.setCellStyle(cellStyle);

        cell = row.createCell(1);
        cell.setCellValue("就餐时间");
        cell.setCellStyle(cellStyle);

        cell = row.createCell(2);
        cell.setCellValue("次数");
        cell.setCellStyle(cellStyle);

        cell = row.createCell(3);
        cell.setCellValue("就餐标准");
        cell.setCellStyle(cellStyle);

        cell = row.createCell(4);
        cell.setCellValue("金额");
        cell.setCellStyle(cellStyle);

        cell = row.createCell(5);
        cell.setCellValue("合计金额");
        cell.setCellStyle(cellStyle);

        cell = row.createCell(6);
        cell.setCellValue("经费来源");
        cell.setCellStyle(cellStyle);

        cell = row.createCell(7);
        cell.setCellValue("备注");
        cell.setCellStyle(cellStyle);
        // ******************************数据*************************************

        cellStyle = PoiUtil.cellStyle1(workbook);
        CellStyle cellStyle2 = PoiUtil.cellStyle2(workbook);

        sheet.addMergedRegion(new CellRangeAddress(3, 5, 0, 0));
        row = sheet.createRow(3);
        row.setHeightInPoints(16.2F);
        cell = row.createCell(0);
        cell.setCellValue("借调人员");
        cell.setCellStyle(cellStyle);
        cell = row.createCell(1);
        cell.setCellValue("早");
        cell.setCellStyle(cellStyle);
        cell = row.createCell(2);
        cell.setCellValue("0");
        cell.setCellStyle(cellStyle);
        cell = row.createCell(3);
        cell.setCellValue("9.00,6.00");
        cell.setCellStyle(cellStyle);
        cell = row.createCell(4);
        cell.setCellValue("0");
        cell.setCellStyle(cellStyle);
        cell = row.createCell(5);
        sheet.addMergedRegion(new CellRangeAddress(3, 5, 5, 5));
        cell.setCellValue("0");
        cell.setCellStyle(cellStyle);
        cell = row.createCell(6);
        sheet.addMergedRegion(new CellRangeAddress(3, 5, 6, 6));
        cell.setCellValue("□计划管理费\n□行政经费\n□其他：");
        cell.setCellStyle(cellStyle2);

        sheet.addMergedRegion(new CellRangeAddress(3, 5, 7, 7));
        cell = row.createCell(7);
        cell.setCellStyle(cellStyle);

        row = sheet.createRow(4);
        row.setHeightInPoints(16.2F);

        cell = row.createCell(0);
        cell.setCellStyle(cellStyle);
        cell = row.createCell(1);
        cell.setCellValue("午");
        cell.setCellStyle(cellStyle);
        cell = row.createCell(2);
        cell.setCellValue("0");
        cell.setCellStyle(cellStyle);
        cell = row.createCell(3);
        cell.setCellValue("9.00,6.00");
        cell.setCellStyle(cellStyle);
        cell = row.createCell(4);
        cell.setCellValue("0");
        cell.setCellStyle(cellStyle);
        cell = row.createCell(5);
        cell.setCellValue("0");
        cell.setCellStyle(cellStyle);
        cell = row.createCell(6);
        cell.setCellStyle(cellStyle);
        cell = row.createCell(7);
        cell.setCellStyle(cellStyle);

        row = sheet.createRow(5);
        row.setHeightInPoints(16.2F);
        cell = row.createCell(0);
        cell.setCellStyle(cellStyle);
        cell = row.createCell(1);
        cell.setCellValue("晚");
        cell.setCellStyle(cellStyle);
        cell = row.createCell(2);
        cell.setCellValue("0");
        cell.setCellStyle(cellStyle);
        cell = row.createCell(3);
        cell.setCellValue("9.00,6.00");
        cell.setCellStyle(cellStyle);
        cell = row.createCell(4);
        cell.setCellValue("0");
        cell.setCellStyle(cellStyle);
        cell = row.createCell(5);
        cell.setCellStyle(cellStyle);
        cell = row.createCell(6);
        cell.setCellStyle(cellStyle);
        cell = row.createCell(7);
        cell.setCellStyle(cellStyle);

        try (FileOutputStream fos = new FileOutputStream("C:\\Users\\sjt\\Desktop\\1.xlsx")) {
            workbook.write(fos);
            workbook.close();
        } catch (Exception ignore) {
        }
        System.out.println("Excel文件创建成功！");
    }
}
